package com.chixing.service.impl;


import com.chixing.pojo.Myorder;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * Created by BSY on2021/10/22 20:54
 */
@Service
public class MsgSender {
    @Autowired
    private RabbitTemplate rabbitTemplate;

    public void addVipOrderMsg(Myorder myorder, long delayedTime) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        CorrelationData correlationData = new CorrelationData(String.valueOf(1001));
        rabbitTemplate.convertAndSend("ORDER_EXCHANGE", "KEY", myorder,
                message -> {
                    // 设置半小时过期
                    message.getMessageProperties().setHeader("x-delay", delayedTime * 1000);
                    return message;
                },
                correlationData);
        System.out.println("订单延迟发送成功：" + sdf.format(new Date()));
    }
}